<template>
  <div class="register-container">
    <h1>注册</h1>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          id="username" 
          v-model="form.username" 
          type="text" 
          placeholder="请输入用户名"
          required
        >
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          id="password" 
          v-model="form.pwd" 
          type="password" 
          placeholder="请输入密码"
          required
        >
      </div>
      <div class="form-group">
        <label for="realName">真实姓名</label>
        <input 
          id="realName" 
          v-model="form.realName" 
          type="text" 
          placeholder="请输入真实姓名"
          required
        >
      </div>
      <div class="form-group">
        <label for="stuNum">学号</label>
        <input 
          id="stuNum" 
          v-model="form.stuNum" 
          type="text" 
          placeholder="请输入学号"
          required
        >
      </div>
      <div class="form-group">
        <label for="className">班级</label>
        <input 
          id="className" 
          v-model="form.className" 
          type="text" 
          placeholder="请输入班级"
          required
        >
      </div>
      <div class="form-group">
        <label for="email">邮箱</label>
        <input 
          id="email" 
          v-model="form.email" 
          type="email" 
          placeholder="请输入邮箱"
          required
        >
      </div>
      <div class="form-group">
        <label for="age">年龄</label>
        <input 
          id="age" 
          v-model="form.age" 
          type="number" 
          placeholder="请输入年龄"
          required
        >
      </div>
      <button type="submit" class="register-btn">注册</button>
    </form>
    <p class="login-link">
      已有账号？<router-link to="/login">立即登录</router-link>
    </p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '../composables/useApi'

const form = ref({
  username: '',
  pwd: '',
  realName: '',
  stuNum: '',
  className: '',
  email: '',
  age: ''
})

const router = useRouter()

const handleRegister = async () => {
  try {
    await api.register(form.value)
    alert('注册成功，请登录')
    router.push('/login')
  } catch (error) {
    alert('注册失败: ' + error.response?.data?.message || error.message)
  }
}
</script>

<style scoped>
.register-container {
  max-width: 500px;
  margin: 2rem auto;
  padding: 2rem;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

button {
  width: 100%;
  padding: 0.75rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 1rem;
  background-color: #42b983;
  color: white;
}

.login-link {
  text-align: center;
  margin-top: 1rem;
}

a {
  color: #42b983;
  text-decoration: none;
}
</style>